//
//  DrawGesturePasswordVC.m
//  MRBang
//
//  Created by 铅笔 on 16/4/28.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "DrawGesturePasswordVC.h"

@interface DrawGesturePasswordVC ()

@end

@implementation DrawGesturePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"手势密码";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    
    [self initWithDrawGesturePasswordVCSubviews];
    
    UIBarButtonItem *button_left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"return_left"] style:UIBarButtonItemStylePlain target:self action:@selector(backUpPrevious)];
    self.navigationItem.leftBarButtonItem = button_left;
    button_left.tintColor = [UIColor whiteColor];
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;

}

//返回上一个页面
- (void) backUpPrevious
{
//    if ([self.str_fromWhere isEqualToString:@"meCenterGesture"]) {
//    
//      [self.navigationController popToRootViewControllerAnimated:YES];
//    }
//    else
//    {
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"indexTabbar"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
        [self.navigationController popViewControllerAnimated:YES];
//    }

}

#pragma mark --- 创建页面
- (void) initWithDrawGesturePasswordVCSubviews
{

    UILabel * label_tips = [[UILabel alloc] initWithFrame:CGRectMake(30*PROPORTION_WIDTH, 64+100, kScreenWidth-30*PROPORTION_WIDTH*2,30*PROPORTION_WIDTH)];
    [self.view addSubview:label_tips];
    label_tips.textAlignment = NSTextAlignmentCenter;
    label_tips.textColor = COLOT_TEXTGRAY;
    label_tips.font = [UIFont systemFontOfSize:Font_SIZE_24];
    
    [DBGuestureLock clearGuestureLockPassword]; //just for test
    
    //Working with block:
    DBGuestureLock *lock = [DBGuestureLock lockOnView:self.view onPasswordSet:^(DBGuestureLock *lock, NSString *password) {
        if (lock.firstTimeSetupPassword == nil) {
            lock.firstTimeSetupPassword = password;
            NSLog(@"varify your password");
            
            label_tips.text = @"请再次绘制解锁图案";
        }
    } onGetCorrectPswd:^(DBGuestureLock *lock, NSString *password) {
        if (lock.firstTimeSetupPassword && ![lock.firstTimeSetupPassword isEqualToString:DBFirstTimeSetupPassword]) {
            lock.firstTimeSetupPassword = DBFirstTimeSetupPassword;
            NSLog(@"password has been setup!");

            label_tips.text = @"设置成功!";
            [UIView showAlertMsg:@"设置成功!"];
            
//            if ([self.str_fromWhere isEqualToString:@"meCenterGesture"]) {
//                [self.navigationController popToRootViewControllerAnimated:YES];
//            }
//            else
//            {

            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"indexTabbar"];
            [[NSUserDefaults standardUserDefaults] synchronize];

            [self.navigationController popViewControllerAnimated:YES];
//            }
            
        } else {
            NSLog(@"login success");
        }
    } onGetIncorrectPswd:^(DBGuestureLock *lock, NSString *password) {
        if (![lock.firstTimeSetupPassword isEqualToString:DBFirstTimeSetupPassword]) {
            NSLog(@"Error: password not equal to first setup!");
            
            label_tips.text = @"两次绘制不一样";
            //两次密码不一致则重头开始
            [DBGuestureLock clearGuestureLockPassword]; //just for test
            lock.firstTimeSetupPassword = nil;
            
        } else {
            NSLog(@"login failed");
        }
    }];
    
    [self.view addSubview:lock];
    
    label_tips.text = @"请设置您的手势密码";

    self.view.backgroundColor = COLOR_EVERYVIEWBG;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
